﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

using EnglishRegister.Business;
using EnglishRegister.Utilities;
namespace EnglishRegister.Forms
{
    public partial class MainForm : Form
    {
        private string[] filterTypes = { "All", "Starting Class", "Time To Learn Even", "Time To Learn Uneven" };
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.LoadGridview();
            this.LoadAutoComplete();
            this.LoadFilter();

        }

        private void LoadFilter()
        {
            this.cmbFilter.DataSource = this.filterTypes;
        }


        private void LoadAutoComplete()
        {
            AutoCompleteStringCollection autoStrings = new AutoCompleteStringCollection();
            IList<string> listCourseName = CourseBL.GetAllCourseName();
            foreach (var item in listCourseName)
            {
                autoStrings.Add(item);
            }
            this.tbxSearch.AutoCompleteMode = AutoCompleteMode.Suggest;
            this.tbxSearch.AutoCompleteSource = AutoCompleteSource.CustomSource;
            this.tbxSearch.AutoCompleteCustomSource = autoStrings;
        }

        private void LoadGridview()
        {
            IList listData = CourseBL.GetAllCourse();
            this.grvCourse.DataSource = listData;
            this.grvCourse.Columns["CourseID"].Visible = false;
        }

        private void LoadGridview(IList result)
        {
            this.grvCourse.DataSource = result;
            this.grvCourse.Columns["CourseID"].Visible = false;
        }

        private void tbxSearch_TextChanged(object sender, EventArgs e)
        {
            string strText = this.tbxSearch.Text;
            FiIterType filter = this.GetFilterByIndex(this.cmbFilter.SelectedIndex);
            IList listData = CourseBL.GetAllCourseByFilter(filter, strText);
            if (listData == null) return;

            //if (string.IsNullOrEmpty(strText))
            //    listData = CourseBL.GetAllCourse();
            //else
            //    listData = CourseBL.SearchCourseByName(strText);
            this.grvCourse.DataSource = listData;

        }

        private void grvCourse_DoubleClick(object sender, EventArgs e)
        {
            if (this.grvCourse.SelectedRows == null || this.grvCourse.SelectedRows.Count == 0)
                return;
            DataGridViewRow row = this.grvCourse.SelectedRows[0];
            if (row == null) return;

            Guid CourseID = (Guid)row.Cells["CourseID"].Value;
            if (CourseID == null) return;
            frmCourseDetail courseView = new frmCourseDetail(CourseID);
            this.Hide();
            courseView.ParentForm = this;
            courseView.MdiParent = this.MdiParent;
            courseView.Show();
        }

        private void btnNewCourse_Click(object sender, EventArgs e)
        {
            this.Hide();
            frmCourseDetail frmNewCourse = new frmCourseDetail();
            frmNewCourse.ParentForm = this;
            frmNewCourse.MdiParent = this.MdiParent;
            frmNewCourse.Show();

        }

        private void MainForm_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible)
            {
                this.LoadGridview();
            }
        }

        private FiIterType GetFilterByIndex(int index)
        {
            switch (index)
            {
                case 0:
                    return FiIterType.All;
                case 1:
                    return FiIterType.StartingClass;
                case 2:
                    return FiIterType.TimeEven;
                case 3:
                    return FiIterType.TimeUneven;
                default:
                    return FiIterType.All;
            }
        }

        private void cmbFilter_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cmbFilter = sender as ComboBox;
            this.tbxSearch.Clear();
            FiIterType filter = this.GetFilterByIndex(cmbFilter.SelectedIndex);
            IList data = CourseBL.GetAllCourseByFilter(filter, this.tbxSearch.Text);
            if (data == null) return;
            this.LoadGridview(data);
        }

        private void bntDelete_Click(object sender, EventArgs e)
        {
            if (this.grvCourse.SelectedRows == null || this.grvCourse.SelectedRows.Count == 0)
                return;
            DataGridViewRow row = this.grvCourse.SelectedRows[0];
            if (row == null) return;

            Guid CourseID = (Guid)row.Cells["CourseID"].Value;
            if (CourseID == null || CourseID == Guid.Empty) return;

            if (StudentBL.CheckExistedStudent(CourseID))
            {
                if (DialogResult.Yes == UtilitiesTools.ShowWarning("This class has been registed by student. Do you want to delete?"))
                {
                    CourseBL.DeleteCourse(CourseID);
                }
            }
            else
                CourseBL.DeleteCourse(CourseID);
            this.LoadGridview();
        }

    }
}
